/**
 * <p>Title: Oxidy公共包</p>
 * <p>Description: Oxidy公共包</p>
 * <p>Copyright: Copyright (C) 2008-2010</p>
 * <p>Company: [OXIDY.NF.NET/OXIDY.JAVAEYE.COM]</p>
 * 
 * FileName: CharacterEncodingFilter.java
 * Package: net.sf.oxidy.common.filter
 * Description: 用于设置 HTTP 请求字符编码的过滤器，通过过滤器参数encoding指明使用何种字符编码,
 *              用于处理Html Form请求参数的中文问题
 *              
 * @author zhangyj
 * @version 2008-11-8 下午02:42:14
 */
package net.sf.oxidy.common.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * 用于设置 HTTP 请求字符编码的过滤器，通过过滤器参数encoding指明使用何种字符编码,
 * 用于处理Html Form请求参数的中文问题
 *
 */
public class CharacterEncodingFilter implements Filter {

	/**
	 * The character encoding to set be used for current 
	 * request and response.
	 */
	private String encoding = null;
	
	/**
	 * The filter configuration object we are associated with. If this value
	 * is null, this filter instance is not currently configured.
	 */
	protected FilterConfig filterConfig = null;
	
	/**
	 * Should a character encoding specified by the client be ignored?
	 */
	private boolean ignore = true;
	
	/**
     * Take this filter out of service.
     * 
     * @todo Implement this javax.servlet.Filter method
     */
	public void destroy() {

		this.encoding = null;
		this.filterConfig = null;
	}

	/**
     * This filter sets the character encoding to be used for current
     * request and response.
     *
     * @param request ServletRequest
     * 			The Servlet request we are processing
     * @param result ServletResponse
     * 			The Servlet response we are creating
     * @param chain FilterChain
     * 			The filter chain we are processing
     *
     * @exception IOException 
     * 			if an input/output error occurs
     * @exception ServletException 
     * 			if a servlet error occurs
     * @todo Implement this javax.servlet.Filter method
     */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {

		try {
		  /*encoding = getUserEncoding();	//从用户配置文件中获取当前用户的编码方式
			if (encoding == null) {
				encoding = filterConfig.getInitParameter("encoding");	//应用程序的编码方式
			}
			if (encoding != null) {
				request.setCharacterEncoding(encoding);	//servlet 2.3 新增方法，用于设置request编码方式
				response.setContentType("text/html;charaset=" + encoding);	//设置response编码方式
			}*/
			if(ignore==true || request.getCharacterEncoding()==null){
				String encoding =setCharacterEncoding(request);
				if(encoding !=null){
					request.setCharacterEncoding(encoding);
					response.setContentType("text/html;charaset=" + encoding);
				}
			}
			
			//Pass control on to the next filter
			chain.doFilter(request, response);
		} catch (ServletException sx) {
			filterConfig.getServletContext().log(sx.getMessage());	//记录Servlet异常信息
		} catch (IOException iox) {
			filterConfig.getServletContext().log(iox.getMessage());	//记录IO异常信息
		}
		
	}

	/**
	 * Place this filter into service.
	 * 
	 * @param filterConfig FilterConfig
	 * 				The filter configuration object
	 * @throws ServletException
	 * @todo Implement this javax.servlet.Filter method
	 */
	public void init(FilterConfig filterConfig) throws ServletException {

		this.filterConfig = filterConfig;
		this.encoding = filterConfig.getInitParameter("encoding");
		String value = filterConfig.getInitParameter("ignore");
		if(value==null){   
			this.ignore=true;   
		}else if(value.equalsIgnoreCase("true")){   
			this.ignore=true;   
		}else{   
			this.ignore=false;   
		}
	}

	/**
	 * Get encoding from current user's configuration.
	 * @return the encoding
	 */
	/*private String getUserEncoding() {
		
		return this.encoding;	//得到指定的编码
	}*/
	private String setCharacterEncoding(ServletRequest request) {
		return this.encoding;
	}

}
